package com.andova.empty.app;

import android.app.Activity;

import com.andova.empty.api.IEmptyDelegate;
import com.andova.lifecycle.IDelegate;
import com.andova.lifecycle.ILifecycle;
import com.andova.manager.AndovaModuleManager;

import java.util.List;

/**
 * Created by Administrator on 2018-03-20.
 *
 * @author kzaxil
 * @since 1.0.0
 */
public class EmptyLifecycle implements ILifecycle {
    private IEmptyDelegate mEmptyDelegate;

    public void showErrorView() {
        if (mEmptyDelegate != null) mEmptyDelegate.showErrorView();
    }

    public void showLoadingView() {
        if (mEmptyDelegate != null) mEmptyDelegate.showLoadingView();
    }

    public void showContentView() {
        if (mEmptyDelegate != null) mEmptyDelegate.showContentView();
    }

    public void showEmptyView() {
        if (mEmptyDelegate != null) mEmptyDelegate.showEmptyView();
    }

    private boolean activityValid(Activity activity) {
        return AndovaModuleManager.obj().empty().appOption().validActivityClass() != null
                && AndovaModuleManager.obj().empty().appOption().validActivityClass().contains(activity.getClass());
    }

    @Override
    public void onActivityCreated(Activity activity, List<IDelegate> delegates) {
        if (!activityValid(activity)) return;
        for (IDelegate delegate : delegates) {
            if (delegate instanceof IEmptyDelegate) mEmptyDelegate = (IEmptyDelegate) delegate;
        }
    }

    @Override
    public void onActivityStarted(Activity activity, List<IDelegate> delegates) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {
        AndovaModuleManager.obj().empty().removeOption(activity);
        AndovaModuleManager.obj().empty().removeIdList(activity);
        if (!activityValid(activity)) return;
        if (mEmptyDelegate != null) {
            mEmptyDelegate.onDestroyView();
            mEmptyDelegate = null;
        }
    }
}
